<?php
namespace app\admin\controller;

use think\Controller;
use app\admin\service\AdminService;
use think\Request;


class Login extends Controller
{
    private $adminService;

    /**
     * 依赖注入 Service
     *
     * @param AdminService $adminService
     * @author zhouyao <994914376@qq.com>
     */
    public function __construct(AdminService $adminService)
    {
        $this->adminService = $adminService;
        parent::__construct();
    }

    /**
     * 显示管理员列表
     * User: zhouyao
     * Date: 2018/9/8
     * Time: 下午1:19
     * @return \think\response\Json
     * @throws \app\common\exception\AppException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $input = $this->request->input;
        $result = $this->adminService->index($input);
        return app_response(200, $result);
    }

    /**
     * 后台登录操作管理
     * User: zhouyao
     * Date: 2018/9/8
     * Time: 下午12:14
     * @return \think\response\Json
     * @throws \app\common\exception\AppException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function postLogin()
    {
        // 接收参数
        $input = $this->request->input;
        $result = $this->adminService->login($input);
        return app_response(200, $result);
    }

    /**
     * 添加管理员用户
     * User: zhouyao
     * Date: 2018/9/8
     * Time: 下午12:43
     * @return \think\response\Json
     * @throws \app\common\exception\AppException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function store()
    {
        $input = $this->request->input;
        $result = $this->adminService->store($input);
        return app_response(200, $result);
    }

    /**
     * 查看当前用户信息
     * User: zhouyao
     * Date: 2018/9/8
     * Time: 下午12:47
     * @return \think\response\Json
     * @throws \app\common\exception\AppException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        $input = $this->request->input;
        $result = $this->adminService->edit($input);
        return app_response(200, $result);
    }

    /**
     * 修改管理员信息
     * User: zhouyao
     * Date: 2018/9/8
     * Time: 下午1:12
     * @return \think\response\Json
     * @throws \app\common\exception\AppException
     */
    public function update()
    {
        $input = $this->request->input;
        $result = $this->adminService->update($input);
        $code = 200;
        if ($result === false) {
            $code = 7996;
        }
        return app_response($code, $result ? '编辑成功' : '编辑失败');
    }

    /**
     * 用户详情
     * User: zhouyao
     * Date: 2018/9/9
     * Time: 上午11:40
     * @return \think\response\Json
     * @throws \app\common\exception\AppException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getInfo()
    {
        $input = $this->request->input;
        $result = $this->adminService->getInfo($input);
        $code = 200;
        return app_response($code, $result);
    }


    public function getDuties()
    {
        return app_response(200, [
            ['id' => 1, 'role' => '超级管理员'],
            ['id' => 2, 'role' => '普通编辑']
        ]);
    }

    /**
     * 编辑用户状
     * User: zhouyao
     * Date: 2018/9/9
     * Time: 下午12:50
     * @return \think\response\Json
     * @throws \app\common\exception\AppException
     */
    public function changeStatus()
    {
        $input = $this->request->input;
        $result = $this->adminService->changeStatus($input);

        return app_response(200, $result ? '编辑成功' : '编辑失败');
    }

}